package com.team.game.Screens;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Assets;
import com.team.game.Game.Gestures;
import com.team.game.Game.Wizard;
import com.team.game.framework.Game;
import com.team.game.framework.Level;
import com.team.game.framework.Screen;

import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;
import static com.team.game.framework.impl.Scale_X_Y.scale;

public class Upgrade_Screen  extends Screen{

    public final static RectF[] icons = new RectF[8];


    private Bitmap bmp_background,  bmp_gold, bmp_diamond, bmp_button, bmp_name_bar, bmp_cost_bar;
    private final RectF rect_background, rect_gold, rect_diamond, rect_button, rect_name_bar, retc_mag, rect_cost_bar;
    private Rect rect_name, rect_cost;

    private Bitmap BmpFrame, BmpPictureFrame;
    private RectF rect_Frame;
    public static RectF rect_PictureFrame;

    private int MagSpellLevel[][] = new int[8][9];

    private int CurrentMag = 0;
    private int CurrentSpell = 0;
    private int CurrentCost = 0;
    private RectF UpgradeSpell[] = new RectF[8];

    private int diamond = 0;
    private int gold    = 0;

    private Paint pnt_gold = new Paint();
    private Paint pnt_mag  = new Paint();
    private ArrayList<Wizard> Wizards = new ArrayList<>(2);

    private ArrayList<Bitmap> upgrade = new ArrayList<>(5);

    public Upgrade_Screen(Game game)
    {
        super(game);
        InputStream inputStream = null;
        try {
            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/Frame.png" );
            BmpFrame = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open("Picture/Upgrade_Screen/PictureFrame.png");
            BmpPictureFrame = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/background.png" );
            bmp_background = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/Almaz.png" );
            bmp_diamond = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/Gold.png" );
            bmp_gold = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/upgrade.png" );
            bmp_button = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/name_bar.png" );
            bmp_name_bar = BitmapFactory.decodeStream(inputStream);

            inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/cost_bar.png" );
            bmp_cost_bar = BitmapFactory.decodeStream(inputStream);

            for (int i = 0; i < 5; i++) {
                inputStream = MainActivity.manager.open ("Picture/Upgrade_Screen/upgrade/upgrade_" + i + ".png");
                upgrade.add (BitmapFactory.decodeStream (inputStream));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        rect_background = new RectF (4   * Scale_X, 77  * Scale_Y, 1075* Scale_X, 1659* Scale_Y);
        rect_gold       = new RectF (743 * Scale_X, 34  * Scale_Y, 936 * Scale_X, 105 * Scale_Y);
        rect_diamond    = new RectF (143 * Scale_X, 34  * Scale_Y, 336 * Scale_X, 105 * Scale_Y);
        rect_button     = new RectF (220 * Scale_X, 1493* Scale_Y, 859 * Scale_X, 1599* Scale_Y);
        rect_name_bar   = new RectF (271 * Scale_X, 434 * Scale_Y, 808 * Scale_X, 591 * Scale_Y);
        retc_mag        = new RectF (470 * Scale_X, 306 * Scale_Y, 609 * Scale_X, 445 * Scale_Y);
        rect_cost_bar   = new RectF (832 * Scale_X, 1370* Scale_Y, 981 * Scale_X, 1448* Scale_Y);
        rect_Frame      = new RectF (453 * scale, 617 * scale, 1002 * scale, 1182 * scale);
        rect_PictureFrame = new RectF(487 * scale,  676 * scale, 970 * scale, 1151* scale);

        rect_name = new Rect ();
        rect_cost = new Rect ();


        for(int i = 0; i < 1; i++)
            Wizards.add(new Wizard(i));
        game.getDatabase().getMagInfo( MagSpellLevel );
        for(int i = 0; i < 8 ; i++)
            UpgradeSpell[i] = new RectF (225 * scale, 657 * scale + i * 105 * scale, 321 * scale, 714 * scale + i * 105 * scale);


        gold = game.getDatabase().getCurrentGold();
        pnt_gold.setColor(Color.BLACK);
        pnt_gold.setTextSize(7*Scale_X_Y.relation);


        pnt_mag.setColor(Color.BLACK);
        pnt_mag.setTextSize(11*Scale_X_Y.relation);
        pnt_mag.getTextBounds (Wizards.get (CurrentMag).MagName, 0, Wizards.get (CurrentMag).MagName.length (), rect_name);

        pnt_gold.getTextBounds (""+CurrentCost, 0, String.valueOf (CurrentCost).length (), rect_cost);

    }
    @Override
    public void update() {
        Wizards.get(CurrentMag).updateProfile(CurrentSpell);
    }

    @Override
    public void present(Canvas canvas) {

        canvas.drawRGB (33, 20, 1);
        canvas.drawBitmap(bmp_background,null, rect_background, null);
        canvas.drawBitmap(bmp_gold,      null, rect_gold,       null);
        canvas.drawBitmap(bmp_diamond,   null, rect_diamond,    null);
        canvas.drawBitmap(bmp_button,    null, rect_button,     null);

        canvas.drawBitmap(Wizards.get(CurrentMag).Mage,null,retc_mag,null);

        canvas.drawBitmap (bmp_name_bar, null, rect_name_bar, null);
        canvas.drawBitmap (bmp_cost_bar, null, rect_cost_bar, null);


        canvas.drawBitmap(BmpFrame, null, rect_Frame,null);
//        canvas.drawBitmap(BmpPictureFrame, null, rect_PictureFrame,null);
        canvas.drawText (""+diamond, 224 * Scale_X, 80 * Scale_Y, pnt_gold);
        canvas.drawText (""+gold   , 820 * Scale_X, 80 * Scale_Y, pnt_gold);

        canvas.drawText (Wizards.get (CurrentMag).MagName, (543*Scale_X)-rect_name.width ()/2, (523 * Scale_Y)+rect_name.height ()/2, pnt_mag);


        //905, 1421
        if(MagSpellLevel[CurrentMag][CurrentSpell + 1] != 4)
            canvas.drawText (""+CurrentCost, (rect_cost_bar.centerX ()-30*Scale_X) -rect_cost.width ()/2, (rect_cost_bar.centerY () + 15*Scale_Y)+rect_cost.height ()/2, pnt_gold);

        Wizards.get(CurrentMag).drawProfile(canvas);

        //canvas.drawBitmap (upgrade.get (), null, Wizard.icons[]);
        
        for(int i = 0; i < 8; i++)
            canvas.drawBitmap(upgrade.get(MagSpellLevel[CurrentMag][i+1]), null,UpgradeSpell[i], null);

    }

    @Override
    public void pause() {
        game.getDatabase ().updateGold (gold);
        game.getDatabase ().updateMagSkills (MagSpellLevel);
    }

    @Override
    public void resume() {

    }

    @Override
    public void dispose() {


    }

    @Override
    public void back() {
        game.setScreen (new Maps_Screen (game));
    }

    @Override
    public Level getLevel() {
        return null;
    }

    @Override
    public Gestures getGestures() {
        return null;
    }

    @Override
    public boolean touchEvent(MotionEvent motionEvent) {

        if (motionEvent.getAction () == MotionEvent.ACTION_UP) {

            for (int i = 0; i < 8; i++) {
                if (Assets.inRect (icons[i], motionEvent)) {
                    if(MagSpellLevel[CurrentMag][i + 1] != 4) {
                        CurrentSpell = i;
                        CurrentCost = Wizards.get(CurrentMag).getCost(i, MagSpellLevel[CurrentMag][i + 1]);
                    }
                }
            }


            if (Assets.inRect (rect_button, motionEvent)) {
                if (gold >= CurrentCost) {
                    if(MagSpellLevel[CurrentMag][CurrentSpell + 1] != 4) {
                        gold -= CurrentCost;
                        MagSpellLevel[CurrentMag][CurrentSpell + 1] += 1;
                        if( MagSpellLevel[CurrentMag][CurrentSpell + 1]!= 4)
                            CurrentCost = Wizards.get(CurrentMag).getCost(CurrentSpell, MagSpellLevel[CurrentMag][CurrentSpell + 1]);
                    }
                }
            }

            return false;
        }
        return false;
    }


}
